
//#include "main.h"
#include "Debug.h"
#include "Outputs.h"
#include "ex_eeprom.h"
#include "ModemService.h"
#include "InputsHandlers.h"
#include "string.h"
#include "my_macros.h"
#include "AuxPort.h"
#include "USART.h"
#include "Inputs.h"
#include "SysTime.h"
#include "Tools.h"
#include "ext_e2_map.h"
#include "PhoneNumbers.h"
#include "Command.h"
#include "Application.h"
#include "consts.h"

/* Alarms */
#define   ALARM_ON_FALL				0x00
#define   ALARM_ON_RISE				0x01
#define   ALARM_ON_ANY_CHANGE		0xFF
#define   RESET_ALL_HANDLERS		0xFF

extern void (*Execute_Module_1) (void);
	
//unsigned char ListNumber;

signed char get_group_number ( char *m )
{
	unsigned char* p = FindCharNumber(m, '[', 1);
	
	if ( p ) 
	{
		return StrToLong(p);
	}

	return -1;
}

typedef struct 
{
	unsigned long inp_name;
	unsigned char state;
	unsigned char event;
	unsigned char cmd_num;
}input_handlers_t;

input_handlers_t inp_handlers[INPUTS_DESCRIPTOR_COUNT];

signed char ih_index (unsigned long inp_name )
{
	unsigned char i;
	for ( i = 0; i < INPUTS_DESCRIPTOR_COUNT; i++ )
	{
		if ( inp_name == inp_handlers[i].inp_name )
		{
			return i;
		}
	}

	return -1;
}

#define   IH_STATE_EVENT_PARSING		0
#define   IH_STATE_ADD_COMMAND		1
#define   IH_STATE_MSG_SEND			2
#define   IH_STATE_FINISH				3

void ih_registration ( void )
{
	unsigned char i, j=0;
	INPUTS_DESCRIPTOR id;
	
	dbg("\n==== ih_registration.start\n"); //Dima
	for ( i = 0; i < INPUTS_DESCRIPTOR_COUNT; i++ )
	{
		if ( Ex_Entry_Read_ByIndex(DIRECTORY__INPUTS_DESCRIPTORS_LIST, (unsigned char*)&id, i) > 0 )
		{
			inp_handlers[j].inp_name = id.InputName;
			dbg("inp_handlers[%d].inp_name = %l\n", j, inp_handlers[j].inp_name); //Dima
			j++;
		}
	}
	dbg("==== ih_registration.exit\n"); //Dima
}

#define   IH_STATE		inp_handlers[i].state
#define   IH_NAME			inp_handlers[i].inp_name
#define   IH_EVENT		inp_handlers[i].event
#define   IH_CMD_NUM		inp_handlers[i].cmd_num



/* typedef struct */
/* { */
/* 		unsigned long InputName;  		/\* Input number *\/ */
/* 		unsigned char EventsOnEdge;/\* 'RISE'(1), 'FALL'(0), 'PULSE'(EventsOnEdge > 1), 'ANY CHANGE'(EventsOnEdge==0xFF)*\/ */
/* 	   unsigned char out_num; */
/* 		unsigned int  out_pulse;  /\* in ms *\/ */
/* 		unsigned char Message[INPUTS_DESCRIPTOR_MESSAGES_COUNT][INPUTS_DESCRIPTOR_MESSAGE_SIZE+1]; */

const char* cmd_list_arm_mode[]=
{
	"MO",
	NULL
};

unsigned char exe_cmd ( char *cmd )
{
	unsigned char i;

	if ( SysState == STATE_ARM )
		return 1;
	
	for(i = 0; cmd_list_arm_mode[i]; i++)
	{
		if ( cmd_list_arm_mode[i][0] == cmd[0] && cmd_list_arm_mode[i][1] == cmd[1] )
		{
			return 1;
		} 
	}
	
	return 0;
}

void InpHandler( unsigned char i )
{	
	INPUTS_DESCRIPTOR id;

	switch ( IH_STATE )
	{
		case IH_STATE_EVENT_PARSING:
			id.InputName = IH_NAME;
			
			dbg("\n==> InpHandler(); input: %d\n", IH_NAME); //Dima

			if ( Ex_Entry_Read(DIRECTORY__INPUTS_DESCRIPTORS_LIST, (unsigned char*)&id) > 0 )
			{				
				dbg("\nid.InputName: 	 %l\n", id.InputName); //Dima
				dbg("id.EventsOnEdge: %d\n", id.EventsOnEdge); //Dima
				
				dbg("IH_EVENT: %d",IH_EVENT); //Dima

				if ( exe_cmd(id.commands[0]) && (id.EventsOnEdge == 255 || id.EventsOnEdge == IH_EVENT-1) )
				{
					IH_STATE = IH_STATE_ADD_COMMAND;
				}
				else
				{
					IH_STATE = IH_STATE_FINISH;
				}
			}
			else
			{
				dbg("\n===> Ex_Entry_Read: FAIL\n"); //Dima
				IH_STATE = IH_STATE_FINISH;
			}
			break;

		case IH_STATE_ADD_COMMAND:
			if ( Execute_Module_1 == NULL )
			{
				id.InputName = IH_NAME;
				Ex_Entry_Read(DIRECTORY__INPUTS_DESCRIPTORS_LIST, (unsigned char*)&id);
				
				dbg("\nid.commands[IH_CMD_NUM]: %s", id.commands[IH_CMD_NUM]); //Dima
				if ( id.commands[IH_CMD_NUM][0] != 0 )
				{
					ExeSysCommand(id.commands[IH_CMD_NUM]);
				}
					  
				if ( ++IH_CMD_NUM == INPUTS_DESCRIPTOR_COMMANDS_COUNT )
				{
					IH_STATE = IH_STATE_FINISH;
				}
			}
			break;

		case IH_STATE_FINISH:
			dbg("\nIH_STATE_FINISH\n"); //Dima
			IH_CMD_NUM = 0;
			IH_STATE = 0;
			IH_EVENT = 0;
			break;

		default:
			dbg("IH_STATE: ERROR!\n"); //Dima;
	}
}

/* 

==== Input: 5; DOWN =====

==> InpHandler(); input: 5

===> Ex_Entry_Read: FAIL

IH_STATE_FINISH

 */

void InputsManager ( void )
{
	static unsigned char State = 0;
	unsigned char i;

	if ( InputsEvent )
	{
		dbg("\n ==== Input: %l; %s =====\n", (InputsEvent & 0x7FFFFFFF), (InputsEvent & INPUT_UP)?"UP":"DOWN");
		
		i = ih_index(InputsEvent & 0x7FFFFFFF);
				
		if ( i == 255 )
		{
			dbg("\nNOT FOUND DESCRIPTOR OF INP: %d\n\n", InputsEvent); 
			return ;
		}
					
		inp_handlers[i].event = 1 + ((InputsEvent & INPUT_UP)?1:0);
	}
	

	for ( i = 0; i < INPUTS_DESCRIPTOR_COUNT; i++ )
	{
		if ( inp_handlers[i].event )
		{
			InpHandler(i);
		}
	}
}





























		/* case IH_STATE_MSG_SEND: */
		/* 	/\* if ( SMS_Sender_Manager__Status != BUSY  ) *\/ */
		/* 	/\* { *\/ */
		/* 	/\* 	id.InputName = IH_NAME; *\/ */
		/* 	/\* 	if ( Ex_Entry_Read(DIRECTORY__INPUTS_DESCRIPTORS_LIST, (unsigned char*)&id) == EX_ERROR ) *\/ */
		/* 	/\* 	{ *\/ */
		/* 	/\* 		Debug("\nERROR: GET LIST_INPUTS_DESCRIPTORS %d\n", id.InputName); *\/ */
		/* 	/\* 		IH_STATE = IH_STATE_FINISH; *\/ */
		/* 	/\* 		return ; *\/ */
		/* 	/\* 	} *\/ */

		/* 	/\* 	SMS_Sender_Manager__SendTo = ListNumber_ToAddress( *\/ */
		/* 	/\* 		get_group_number(id.Message[IH_MSG_NUM])); *\/ */

		/* 	/\* 	if ( SMS_Sender_Manager__SendTo == UNKNOWN_GROUP ) *\/ */
		/* 	/\* 	{ *\/ */
		/* 	/\* 		Debug("\nERROR: UNKNOWN GROUP: %d\n", SMS_Sender_Manager__SendTo);  *\/ */
		/* 	/\* 		IH_STATE = IH_STATE_FINISH; *\/ */
		/* 	/\* 		return ; *\/ */
		/* 	/\* 	} *\/ */
				
		/* 	/\* 	Write_SMS(id.Message[IH_MSG_NUM]); *\/ */

		/* 	/\* 	if ( ++IH_MSG_NUM > INPUTS_DESCRIPTOR_MESSAGES_COUNT ) *\/ */
		/* 	/\* 	{ *\/ */
		/* 	/\* 		IH_STATE = IH_STATE_FINISH; *\/ */
		/* 	/\* 	} *\/ */
		/* 	/\* 	else *\/ */
		/* 	/\* 	{ *\/ */
		/* 	/\* 		SMS_Sender_Manager__Status = BUSY; *\/ */
		/* 	/\* 		Tools = SMS_Sender_Manager; *\/ */
		/* 	/\* 	} *\/ */
		/* 	/\* } *\/ */
		/* 	break; */
